package com.obelisco.modelo.dao.hibernate;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.obelisco.exception.ObeliscoException;
import com.obelisco.modelo.dao.ITransaccionDAO;
import com.obelisco.modelo.data.CarpetaTransaccion;
import com.obelisco.modelo.data.DataGenerica;
import com.obelisco.modelo.data.Sistema;
import com.obelisco.modelo.data.Transaccion;

public class TransaccionDAO extends HibernateDAO  implements
		ITransaccionDAO {

	public Transaccion findByPK(Serializable clave) {
		return  (Transaccion) this.getSession().load(Transaccion.class, clave);
	}
	

	public void delete(Transaccion objeto) {
		delete(objeto);		
	}
	
	ClassValidator transaccionValidator = new ClassValidator(Transaccion.class,ResourceBundle.getBundle("com.obelisco.modelo.data.hibernate-validator"));
	
	public void validate(Transaccion objeto) {

		InvalidValue[] validationMessages =  transaccionValidator.getInvalidValues(objeto);

		StringBuffer errores = new StringBuffer();
		for (int i = 0; i < validationMessages.length; i++) {
			InvalidValue value = validationMessages[i];
			errores.append(value.getPropertyName() + " - "+ value.getMessage() + "<br/>");
		}

		if (errores.length() > 0) {
			
			throw new ObeliscoException(errores.toString());
		}

	}

	public void save(Transaccion objeto) {
		validate(objeto);
		saveOrUpdate(objeto);		
	}

	public List loadAll() {
		try {
			List lista = find(
			"from Transaccion transaccion where transaccion.estado != 'B'");
			return lista;
		} catch (RuntimeException re) {
			throw re;
		}			}

	public List findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from Transaccion modelo where modelo." + propertyName
					+ "= ?";
			return find(queryString, value);
		} catch (RuntimeException re) {
			// log.error("find by property name failed", re);
			throw re;
		}
	}	

}
